package com.yang.controller.SendController;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

/**
 * @author yanfanghao
 */
@Configuration
@EnableAsync
public class ThreadConfig {

    @Bean(name = "asyncExecutor")
    public Executor threadPoolTaskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
//        int kernelCount = Runtime.getRuntime().availableProcessors();
        //核心线程数
        executor.setCorePoolSize(20);
        //最大线程数
        executor.setMaxPoolSize(32);
        //额外线程等待时间
        executor.setKeepAliveSeconds(600);
        //队列长度
        executor.setQueueCapacity(50);
        //线程名称
        executor.setThreadNamePrefix("sjhl-asyncExecutor");
        //拒绝策略
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.initialize();
        return executor;
    }

}
